#include "fig_pickup_animation.h"
#include "fighter.h"
#include "gen_game_state.h"
#include "fig_sfx_manager.h"


#include "math/intersection.h"
#include "math/math_core.h"


FIGPickupAnimation::FIGPickupAnimation()
{
	m_SpriteStar.Create("star_pick_up_item");
	m_SizeStar = m_SpriteStar.m_Size;

	m_Sprite1UP.Create("player_up_item");
	m_Size1UP = m_Sprite1UP.m_Size;

	m_SpriteHP.Create("red_cross");
	m_SizeHP = m_SpriteHP.m_Size;

	m_SpriteNuke.Create("nuclear_item");
	m_SizeNuke = m_SpriteNuke.m_Size;

	
	GENGameState* pState = GENGameState::GetGame();
	ui::UIWidget* pDestWidget = pState->GetWidget("ui_special");	
	m_DestinationStar = pDestWidget->GetPosition() + (pDestWidget->GetSize() * 0.5f);

	pDestWidget = pState->GetWidget("player_health");	
	m_Destination1Up = pDestWidget->GetPosition() + (pDestWidget->GetSize() * 0.5f);

	pDestWidget = pState->GetWidget("player_health");	
	m_DestinationHP = pDestWidget->GetPosition() + (pDestWidget->GetSize() * 0.5f);

	pDestWidget = pState->GetWidget("ui_nuclear");	
	m_DestinationNuke = pDestWidget->GetPosition() + (pDestWidget->GetSize() * 0.5f);

	
}

FIGPickupAnimation&		FIGPickupAnimation::Instance()
{
	static FIGPickupAnimation	Inst;
	return Inst;
}


void		FIGPickupAnimation::Render(const math::Vec2& Offset,
									   float Scale)
{
	math::Vec2 Size;
	ui::UISprite* pSprite;
	for(int i = 0; i < (int)m_Positions.size(); i++)
	{
		switch((FIGPickup::PICKUP_TYPES)m_Types[i])
		{
			case FIGPickup::PICKUP_TYPE_STAR:
				Size = m_SizeStar;
				pSprite = &m_SpriteStar;
			break;

			case FIGPickup::PICKUP_TYPE_1UP:
				Size = m_Size1UP;
				pSprite = &m_Sprite1UP;
			break;

			case FIGPickup::PICKUP_TYPE_HEALTH:
				Size = m_SizeHP;
				pSprite = &m_SpriteHP;
			break;

			case FIGPickup::PICKUP_TYPE_NUKE:
				Size = m_SizeNuke;
				pSprite = &m_SpriteNuke;
			break;

			default:
			break;
		}


		pSprite->m_Pos = m_Positions[i];
		pSprite->m_Size = Size * Scale;
		pSprite->m_Rotation = m_Rotations[i];
		pSprite->RenderRotated();

	}
}

void		FIGPickupAnimation::RenderDebug(const math::Vec2& Offset,
											float Scale)
{
	//for(int i = 0; i < (int)m_Positions.size(); i++)
	{
		
	}
}

void		FIGPickupAnimation::Tick(float dt)
{

	math::Vec2 Destination;
	math::Vec2 Size;



	math::Vec2 Dir;
	for(int i = 0; i < (int)m_Positions.size(); i++)
	{
		switch((FIGPickup::PICKUP_TYPES)m_Types[i])
		{
			case FIGPickup::PICKUP_TYPE_STAR:
				Destination = m_DestinationStar;
				Size = m_SizeStar;
			
			break;

			case FIGPickup::PICKUP_TYPE_1UP:
				Destination = m_Destination1Up;
				Size = m_Size1UP;
				
			break;

			case FIGPickup::PICKUP_TYPE_HEALTH:
				Destination = m_DestinationHP;
				Size = m_SizeHP;
			
			break;

			case FIGPickup::PICKUP_TYPE_NUKE:
				Destination = m_DestinationNuke;
				Size = m_SizeNuke;
				
			break;

			default:
			break;
		}

		m_Rotations[i] += 360 * dt;
		if(m_Rotations[i] > 360.0f)
			m_Rotations[i] -= 360.0f;


		Dir = Destination - m_Positions[i];
		Dir.Normalize();

		m_Positions[i] += (Dir * dt * 1.3f);
		const float RADIUS = Dir.Magnitude();
		m_Positions[i].x += (cosf(m_ArcCounters[i] * MATH_2PI )  * RADIUS *  dt ); 
		//m_Positions[i].y += (sinf(m_ArcCounters[i] * MATH_2PI)  * RADIUS *  dt );

		m_ArcCounters[i] += dt;
		if(m_ArcCounters[i] > 1.0f)
			m_ArcCounters[i] = 0;

		if(math::Intersection::PointInsideRect2DFast(	m_Positions[i],
													Destination,
													Size,													
													true
												))
		{
			FIGSFXManager::Instance().Spawn(FIGSfx::PICKUP_TO_UI, 
											Destination ,
											2.5f);

			m_Positions.erase(m_Positions.begin() + i);
			m_Types.erase(m_Types.begin() + i);
			m_ArcCounters.erase(m_ArcCounters.begin() + i);
			m_Rotations.erase(m_Rotations.begin() + i);

			

		}
	}
}

void				FIGPickupAnimation::Animate(const math::Vec2& From,
												FIGPickup::PICKUP_TYPES Type
												)
{
	m_Positions.push_back(From + Fighter::Instance().GetOffset());
	m_Types.push_back((int)Type);
	m_ArcCounters.push_back(0);
	m_Rotations.push_back(0);
}